var loginCustomer=null;
loadInitData();
function loadInitData() {
  var token = localStorage.getItem("token");
  console.log("token:"+token);
  $.ajax({
    type: "get",
    url: "/demo/XQing",
    headers: {'token': token},
    success: function (result) {
        loginCustomer=result.data;
        console.log("==============")
        console.log(result.data)
        if(loginCustomer!=null){
            $(".head-user").hide();
            var loginCustomerId=loginCustomer.customerId;
            var url = "/demo/order?customerId=" + loginCustomerId;
            $(".customerIdDZ").text(loginCustomerId)
            $(".user-menu").html('<li class="text"><a href='+url+'>个人中心</a></li>\n' +
                '                 <li class="text" id="return"><button style="border: none">退出登录</button></li>');
            $(".user-info").show();

        }else {
            $(".head-user").show();
            $(".user-info").hide();
        }
        $("#return").click(function (){
            var token = localStorage.removeItem("token");
            window.location.href = "http://localhost:8080/demo/index.jsp";
        })
    }
  })
}
collectData();
function collectData() {
    var movieId=$(".movieId").text()
    console.log(movieId)
    if(loginCustomer!=null) {
        $.ajax({
            type: "get",
            url: "isCollect?movieId="+movieId+"&customerId="+loginCustomer.customerId,
            success: function (result) {
                if (result===true){
                    $(".wish-msg").text("已收藏")
                    //$(".wish-icon").style.background="url(img/collect_img_1.png)";
                    //$(".wish-icon").css("background","url(http://localhost:8080/demo/img/collect_img_1.png)");
                    $(".wish-icon").css("background","url(../demo/img/collect_img_1.svg)");
                }
            }
        })
    }
}
buyData();
function buyData() {
    var movieStatus=$(".movieStatus").text()
    if (movieStatus===1){
        $(".buy").hide();
    }
}

$(".wish").click(function (){
    if (loginCustomer==null){
        window.location.href = "http://localhost:8080/demo/pages/login.jsp";
    }
    var movieId=$(".movieId").text()
    console.log(movieId)
    var Collect=$(".wish-msg").text();
    var pdCollect;
    if (Collect==="收藏"){
        pdCollect=0;
    }else {
        pdCollect=1;
    }
    $.ajax({
        type: "get",
        url: "pdCollect?movieId="+movieId+"&customerId="+loginCustomer.customerId+"&pdCollect="+pdCollect,
        success: function (result) {
            var resultCode=result.resultCode;
            var data=result.data;
            if (resultCode===200&&data===true){
                $(".wish-msg").text("已收藏")
                //document.querySelector(".wish-icon").style.backgroundImage="url(http://localhost:8080/demo/img/collect_img_1.png)";
                // $(".wish-icon").css("background","url(../img/BangDan_d1.png)");
                $(".wish-icon").css("background","url(../demo/img/collect_img_1.svg)");
            }else if (resultCode===200&&data===false){
                $(".wish-msg").text("收藏")
                $(".wish-icon").css("background","url(../demo/img/collect_img_0.svg)");
            }else if (resultCode===201&&data===true){
                alert("收藏失败")
            }else if (resultCode===201&&data===false){
                alert("取消收藏失败")
            }
        }
    })
})


window.onload = function(){
    //获取弹窗按钮
var btn=document.getElementById("comment-entry");
 var close=document.getElementsByClassName("close")[0];
 console.log("close:"+close)
 var myModal=document.getElementById("myModal");
 //按钮绑定事件
 btn.onclick=function () {
  //获取弹窗
 
  myModal.style.display="block";
 }
 close.onclick=function () {
  myModal.style.display="none";
 }
var btn1=document.getElementsByClassName("fabuBtn")[0];
 console.log(btn1)
 btn1.onclick=function (){
     var comments=document.getElementById("modalContent");
     var commentText=comments.value;
     console.log("comments:"+commentText)
     var token=localStorage.getItem("token");
     console.log("token:"+token);
     var movieIdData=document.querySelector(".movieIdData");
     var movieId=movieIdData.textContent;
     console.log("movieIdData"+movieIdData.textContent)
     myModal.style.display="none";
     $.ajax({
         type: "post",
         url: "comments/saveComment",
         headers: {'token':token},
         data:{
         commentText:commentText,
         movieId:movieId
         },
         success:function (result){
             location.reload(true);
             console.log(result);

         }
     })

 }
 //用户点击其他地方关闭弹窗
 window.onclick=function (event) {
  if(event.target ==myModal){
   myModal.style.display="none";
  }
 }
}

/*$(".buy").click(function (){
 var movieId = $(".movieId").val();
 $.ajax({
  type:"get",
  url:"../cinema_movies?movieId="+movieId,
  success:function (result){

  }
 })
})*/
console.log("=========")
console.log(document.getElementsByClassName("approve-icon"))

function jiazaidianzan(){
    var token=localStorage.getItem("token");
    console.log("=====dianzan====")

    $.ajax({
        type:"get",
        url:"getLikesByCustomerId",
        headers:{'token':token},
        success:function (result) {
            console.log("++++++++++++")
            console.log(result)
            var arr=result.likesList;
            var arr1=result.likesListFromResis;
            var arr3=result.commentsList;
            console.log(arr3)
            arr=arr.concat(arr1);
            var commmentIdList=document.getElementsByClassName("commentId");
            for(var i=0;i<arr.length;i++){
                for(var j=0;j<commmentIdList.length;j++){
                    console.log("commmentIdList[j].textContent:"+commmentIdList[j].textContent)
                    console.log("arr[i].commentsId:"+arr[i].commentsId)
                    if(commmentIdList[j].textContent==arr[i].commentsId&&arr[i].likesStatus==0){
                        //console.log("当前评论已经被当前用户评论过了，按钮变色")
                        var eleDianzan=document.querySelector("i[COMID='"+arr[i].commentsId+"']")
                        eleDianzan.className="approve-icon1";
                    }
                    if(commmentIdList[j].textContent==arr[i].commentsId&&arr[i].likesStatus==1){
                        //console.log("取消点赞")
                        var eleDianzan=document.querySelector("i[COMID='"+arr[i].commentsId+"']")
                        //console.log(eleDianzan)
                        if( eleDianzan.className=="approve-icon1")
                        eleDianzan.className="approve-icon";
                    }
                }
            }
            for(var k=0;k<arr3.length;k++){//redis中点赞数
                     for (var l=0;l<commmentIdList.length;l++) {
                         if(commmentIdList[l].textContent==arr3[k].commentsId){
                             var eleDianzan1=document.querySelector("i[COMID='"+arr3[k].commentsId+"']");
                             var num= eleDianzan1.nextElementSibling.textContent;
                             console.log("NUM"+num)
                             var num1=parseInt(num)+arr3[k].commentsLikes;
                             console.log(num1)
                             eleDianzan1.nextElementSibling.textContent=num1;
                         }

                     }}
            // console.log("===当前用户id==")
            // console.log($(".customerIdDZ").text())
            // console.log("arr1"+arr1.length)
            // for(var k=0;k<arr1.length;k++){//redis中当前用户
            //     for (var l=0;l<commmentIdList.length;l++){//redis中的状态要为0才变红
            //         if(commmentIdList[l].textContent==arr1[k].commentsId&&arr1[k].customerId==$(".customerIdDZ").text()&&arr1[k].likesStatus==0){
            //                 console.log("当前评论已经被当前用户点赞过了，按钮变色")
            //                 var eleDianzan=document.querySelector("i[COMID='"+arr1[k].commentsId+"']")
            //                 console.log(eleDianzan)
            //                 eleDianzan.className="approve-icon1";
            //
            //         }
            //         if(commmentIdList[l].textContent==arr1[k].commentsId&&arr1[k].customerId==$(".customerIdDZ").text()&&arr1[k].likesStatus==1){
            //             console.log("当前评论已经被当前用户取消点赞了，按钮变色")
            //             var eleDianzan=document.querySelector("i[COMID='"+arr1[k].commentsId+"']")
            //             console.log(eleDianzan)
            //             eleDianzan.className="approve-icon";
            //
            //         }
                    // if(commmentIdList[l].textContent==arr1[k].commentsId&&arr1[k].likesStatus==0){//redis中的状态要为0才+1
                    //    var eleDianzan1=document.querySelector("i[COMID='"+arr1[k].commentsId+"']");
                    //    var num= eleDianzan1.nextElementSibling.textContent;
                    //    console.log("NUM"+num)
                    //    var num1=parseInt(num)+1;
                    //    console.log(num1)
                    //     eleDianzan1.nextElementSibling.textContent=num1;
                    // }
                    // if(commmentIdList[l].textContent==arr1[k].commentsId&&arr1[k].likesStatus==1){//redis中的状态要为0才+1
                    //     var eleDianzan1=document.querySelector("i[COMID='"+arr1[k].commentsId+"']");
                    //     var num= eleDianzan1.nextElementSibling.textContent;
                    //     console.log("NUM"+num)
                    //     var num1=parseInt(num)-1;
                    //     if(num1<0)
                    //         num1=0;
                    //     console.log(num1)
                    //     eleDianzan1.nextElementSibling.textContent=num1;
                    // }
        //         }
        //     }
         }
    })
}
jiazaidianzan();//加载当前用户点赞的按钮变红

document.querySelector("#dianzanFather").onclick=function(event){
    console.log("///////////////////////////////////////////")
    var element=event.target;//事件作用的元素
    var nodename=element.nodeName;//标签名称
    console.log(nodename+"111111")
    console.log(element.className)




    var dzflag=0;
    if(element.className=="approve-icon1"||element.className=="approve-icon"){
        if(element.className=="approve-icon1"){
            element.className="approve-icon";
            dzflag=-1;
            element.nextElementSibling.textContent=parseInt(element.nextElementSibling.textContent)-1;
        }
        else {
            element.className="approve-icon1";
            dzflag=1;
            element.nextElementSibling.textContent=parseInt(element.nextElementSibling.textContent)+1;
        }
    }
    var commendId=$(event.target).attr('COMID');
    console.log(commendId)
    var token = localStorage.getItem("token");

    var movieIdData=document.querySelector(".movieIdData");
    var movieId=movieIdData.textContent;
    console.log("movieIdData"+movieIdData.textContent)

    $.ajax({
        type:"get",
        url:"redisDecreaseLike",
        headers: {'token': token},
        data: {
            commentId:commendId,
            likesNum:dzflag,
            movieId:movieId
        },
        success: function (result) {
        console.log("=========ok========")
            console.log(result)
            // var commmentIdList=document.getElementsByClassName("commentId");
            // var arr33=result;
            // for(var ii=0;ii<commmentIdList.length;ii++){
            //     var eleDianzan1=document.querySelector("i[COMID='"+commmentIdList[ii].textContent+"']");
            //     eleDianzan1.nextElementSibling.textContent=0;
            // }
            // for(var k=0;k<arr33.length;k++){//redis中点赞数和数据库的
            //     for (var l=0;l<commmentIdList.length;l++) {
            //         if(commmentIdList[l].textContent==arr33[k].commentsId){
            //             var eleDianzan1=document.querySelector("i[COMID='"+arr33[k].commentsId+"']");
            //             var num= eleDianzan1.nextElementSibling.textContent;
            //             console.log("NUM"+num)
            //             var num1=parseInt(num)+arr33[k].commentsLikes;
            //             console.log(num1)
            //             eleDianzan1.nextElementSibling.textContent=num1;
            //         }
            //     }}


        }
    })

}

$(function() {
    var status = true;
    $("#collection").click(function() {
        if(status) {
            $(this).children("i").html("&#xe627;")
            status = false;
        }else{
            $(this).children("i").html("&#xe666;")
            status = true;
        }
    });
});